package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.api.IArticleClient;
import com.heima.api.INewsClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.ApUserRealnameDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author zqc
 * @since 2024-08-21
 */
@Service
@Slf4j
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {
    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private INewsClient iNewsClient;
    @Autowired
    private IArticleClient iArticleClient;
    private final Short AUTH_AUDIT_PASS = 9;
    private final Short AUTH_AUDIT_FAIL = 2;

    /**
     * 作者认证列表
     *
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult authList(ApUserRealnameDto dto) {
        dto.checkParam();
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(dto.getStatus()), ApUserRealname::getStatus, dto.getStatus());
        page(page, wrapper);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        page.getRecords().stream().filter(m->{
            String idno = m.getIdno();
            String mask = mask(idno, 7, 14, '*');
            m.setIdno(mask);
            return true;
        }).collect(Collectors.toList());
        result.setData(page.getRecords());
        return result;
    }

    public static String mask(String str, int start, int end, char maskChar) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        char[] chars = str.toCharArray();
        for (int i = start; i < end && i < chars.length; i++) {
            chars[i] = maskChar;
        }
        return new String(chars);
    }

    /**
     * 作者申请的审核
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authAudit(ApUserRealnameDto dto) {
        if (ObjectUtils.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(AUTH_AUDIT_PASS);
        if (!StringUtils.isEmpty(dto.getMsg())) {
            apUserRealname.setStatus(AUTH_AUDIT_FAIL);
            apUserRealname.setReason(dto.getMsg());
        }
        apUserRealnameMapper.updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult authFail(ApUserRealnameDto dto) {
        if (ObjectUtils.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(AUTH_AUDIT_FAIL);
        apUserRealname.setReason(dto.getMsg());
        apUserRealname.setUpdatedTime(LocalDateTime.now());
        apUserRealnameMapper.updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult authPass(ApUserRealnameDto dto) {
        if (ObjectUtils.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(AUTH_AUDIT_PASS);
        apUserRealnameMapper.updateById(apUserRealname);
        ApUserRealname realname = apUserRealnameMapper.selectById(apUserRealname.getId());
        Integer userId = realname.getUserId();
        ApUser apUser = apUserMapper.selectById(userId);
        //向自媒体端添加
        WmUser wmUser = new WmUser();
        wmUser.setNickname(realname.getName());
        BeanUtils.copyProperties(apUser,wmUser);
        wmUser.setCreatedTime(new Date());
        wmUser.setApUserId(apUser.getId());
        wmUser.setStatus(9);
        wmUser.setType(0);
        iNewsClient.add(wmUser);
//        Integer wmUserId = (Integer) responseResult.getData();
//        //向app端添加
//        ResponseResult result = iArticleClient.add(userId, wmUserId, realname.getName());
//        Integer authorId = (Integer) result.getData();
//        iNewsClient.update(authorId,wmUserId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
